package com.leave.mos.service.impl;

import com.leave.mos.constant.TablenameConstant;
import com.leave.mos.entity.Order;
import com.leave.mos.entity.QueryParam;
import com.leave.mos.service.find.QueryCriteriaService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName MongodbCriteriaServiceImpl
 * @Description TODO
 * @Author lh
 * @Date 2021/7/8 16:18
 * @Version 1.0
 * <p>
 * 使用 Criteria 对象创建查询条件
 * 字段类型必须保持一致， int  对应 mongodb 中数字类型
 **/
@Service
public class QueryCriteriaServiceImpl implements QueryCriteriaService {


    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 创建一个查询条件  where a = 1 and ( b != 2 or c = 3)
     * Query: { "price" : { "$gte" : 0}, "$and" : [{ "dealName" : { "$ne" : "20170206121427"}}], "$or" : [{ "goodsNum" : 3}]}, Fields: {}, Sort: {}
     * where  a = 1 and b = 3
     * 多条件使用
     */
    @Override
    public List<Order> criteria1(QueryParam queryParam) {

        //
        Criteria criteria = new Criteria(TablenameConstant.Order.price);
        Criteria criteria1 = new Criteria(TablenameConstant.Order.dealName);
        Criteria criteria2 = new Criteria(TablenameConstant.Order.goodsNum);
        criteria.gte(queryParam.getPrice());
       // criteria1.ne(queryParam.getDealTime());
        //多个条件组合 and
        criteria.andOperator(criteria1.ne(queryParam.getDealTime()));
        criteria.orOperator(criteria2.is(queryParam.getGoodsNum()));

        Query query = new Query();
        query.addCriteria(criteria);
        //query.addCriteria(criteria1);
        System.out.println(query.toString());

        List<Order> orders = mongoTemplate.find(query, Order.class, TablenameConstant.Order.TableName);
        return orders;
    }

    /**
     *
     * Query: { "price" : { "$gte" : 0}, "dealName" : { "$ne" : "20170206121421"}, "goodsNum" : 1}, Fields: {}, Sort: {}
     * @param queryParam
     * @return
     */
    @Override
    public List<Order> criteria2(QueryParam queryParam) {

        //创建一个查询条件
        Criteria criteria = new Criteria(TablenameConstant.Order.price);
        Criteria criteria1 = new Criteria(TablenameConstant.Order.dealName);
        Criteria criteria2 = new Criteria(TablenameConstant.Order.goodsNum);

        //多个条件组合 and
        criteria.gte(queryParam.getPrice());
        criteria1.ne(queryParam.getDealTime());
        criteria2.is(queryParam.getGoodsNum());

        Query query = new Query();
        query.addCriteria(criteria);
        query.addCriteria(criteria1);
        query.addCriteria(criteria2);
        System.out.println(query.toString());

        List<Order> orders = mongoTemplate.find(query, Order.class, TablenameConstant.Order.TableName);
        return orders;
    }

    // where id = 1
    @Override
    public List<Order> criteria3(QueryParam queryParam) {

        //JSON 创建一个查询条件
        Criteria criteria = Criteria.where(TablenameConstant.Order.id);
        criteria.is(queryParam.getId());

        Query query = new Query();
        query.addCriteria(criteria);

        System.out.println(query.toString());

        List<Order> orders = mongoTemplate.find(query, Order.class, TablenameConstant.Order.TableName);
        return orders;
    }


    //
    @Override
    public List<Order> criteria4(QueryParam queryParam) {

        //JSON 创建一个查询条件
        Query query = new BasicQuery("{\"dealId\":\"4\"}");

        System.out.println(query.toString());

        List<Order> orders = mongoTemplate.find(query, Order.class, TablenameConstant.Order.TableName);
        return orders;
    }
}
